<?php

namespace App\Console\Commands;

use App\Models\Admin\Tasks;
use App\Models\Home\Orders;
use App\Models\Home\User;
use App\Repositories\TasksRepos;
use App\Repositories\UsersRepos;
use Illuminate\Console\Command;

class CheckTasksState extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'tasks:check';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '实时检测任务状态';

    protected $usersRepos;
    protected $tasksRepos;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct(UsersRepos $usersRepos, TasksRepos $tasksRepos)
    {
        parent::__construct();
        $this->usersRepos = $usersRepos;
        $this->tasksRepos = $tasksRepos;
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //获取已经到截止时间的任务
        $taskModel = Tasks::where('state', '=', 3);
        $taskModel = $taskModel->where('deadlime', '<=', date('Y-m-d H:i:s'));
        $lists = $taskModel->get()->toArray();
        if(!empty($lists) && is_array($lists)) {
            foreach ($lists as $key => $value) {
                $taskDetailModel = Tasks::FindOrFail($value['id']);
                $taskDetailModel->state = 4;
                $taskDetailModel->save();
            }
            echo date('Y-m-d H:i:s')." 执行完成,同步了".count($lists)."任务".PHP_EOL;
        }else{
            echo date('Y-m-d H:i:s')." 没有到期的任务".PHP_EOL;
        }

        //检查是否到达审核时间截止时间
        $taskModel = Tasks::where('state', '=', 4);
        $lists = $taskModel->get()->toArray();
        if (!empty($lists) && is_array($lists)) {
            foreach ($lists as $key => $value) {
                $taskDetailModel = Tasks::FindOrFail($value['id']);
                if (time() >= strtotime($taskDetailModel->deadlime) + $taskDetailModel->audit_time) {
                    $taskDetailModel->state = 5;
                    $taskDetailModel->save();
                    echo date('Y-m-d H:i:s')." 执行完成,审核截止的任务：".$taskDetailModel->title.PHP_EOL;
                    // 过期否决订单
                    $releateTaskOrderLists = Orders::where('task_id',$taskDetailModel->id)->whereIn('state',[1,2])->get()->toArray();
                    if (!empty($releateTaskOrderLists) && is_array($releateTaskOrderLists)) {
                        foreach ($releateTaskOrderLists as $k => $v) {

                            $orderDetailModel = Orders::FindOrFail($v['id']);

                            if ($orderDetailModel->state == 1) {
                                $orderDetailModel->state = 3;
                                $orderDetailModel->save();


                                // 触发消息
                                $taskDetails = $this->tasksRepos->getTaskInfo($orderDetailModel['task_id']);
                                $msgData['${mission_name}'] = $taskDetails['title'];
                                $msgData['${mission_id}'] = $taskDetails['id'];
                                $this->usersRepos->triggerMessage($orderDetailModel['user_id'],'任务过期',$msgData);
                            }

                            if ($orderDetailModel->state == 2) {
                                $orderDetailModel->state = 4;
                                $orderDetailModel->save();

                                $userModel = User::FindOrFail($orderDetailModel['user_id']);
                                $taskDetails = $this->tasksRepos->getTaskInfo($orderDetailModel['task_id']);

                                if (!empty($userModel->recommend_user)) {
                                    // 分提成
                                    $recommendUserModel = User::where('account', $userModel->recommend_user)->first();
                                    if (!empty($recommendUserModel)) {
                                        $rebate = !empty($taskDetails['rebate']) ? $taskDetails['rebate'] : 0;
                                        $recommendUserModel->balance += $rebate;
                                        $recommendUserModel->reward_earnings += $rebate;
                                        $recommendUserModel->save();
                                    }
                                }

                                $userModel->balance = $userModel->balance + $taskDetails['money'];
                                $userModel->task_earnings = $userModel->task_earnings + $taskDetails['money'];
                                $userModel->save();

                                // 触发消息
                                $msgData['${mission_name}'] = $taskDetails['title'];
                                $msgData['${mission_id}'] = $taskDetails['id'];
                                $this->usersRepos->triggerMessage($orderDetailModel['user_id'],'任务审核通过',$msgData);
                            }

                        }
                    }
                }
            }
        }

    }
}
